<?php

namespace app\uniapp\controller;


use app\model\site\Article;
use app\uniapp\service\ArticleService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class ArticleController extends UniController
{
    /**
     * @throws DbException
     */
    public function paginate(): \think\response\Json
    {
        $where = [];
        $cateId = $this->param('cate_id');
        if ($cateId) $where[] = ['cate_id', '=', $cateId];
        $list = ArticleService::instance()->setWhere($where)->paginate(20);
        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total()
        ]);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function find(): \think\response\Json
    {
        $id = $this->param('id');
        $info = ArticleService::instance()->setPkWhere($id)->find();
        return jsonSuccess([
            'info' => $info,
        ]);
    }

}
